package com.eshop.web.controllers.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.eshop.common.constant.CoreConstant;
import com.eshop.common.params.ResetPwdParams;
import com.eshop.common.util.security.MD5;
import com.eshop.frameworks.core.controller.BaseController;
import com.eshop.model.mongodb.EUser;
import com.eshop.service.mongodb.EUserService;

@Controller
//@RequestMapping("/euser")
public class UserInfoController extends BaseController{
	

	@Autowired
	private EUserService euserService;
	
	@RequestMapping("/ucenter")
	public ModelAndView ucenter(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/euser/ucenter.httl");
		setVar(mav);
		EUser user = (EUser) this.getSessionAttribute(request,
				CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			return new ModelAndView("login.httl");
		}
		mav.addObject("user", user);
		mav.addObject("leaf", "center");
		return mav;
	}
	
	@RequestMapping("/euser/bindEmail")
	public ModelAndView bindEmail(HttpServletRequest request,HttpServletResponse response,EUser euser) {
		System.out.println(euser.getEmail());
		ModelAndView mav = new ModelAndView("redirect:/ucenter");
		setVar(mav);
		EUser user = (EUser) this.getSessionAttribute(request,
				CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			return new ModelAndView("login.httl");
		}
		euserService.bindEmail(user.getId(), euser.getEmail());
		user.setEmail(euser.getEmail());
		this.setSessionAttribute(request, response,
				CoreConstant.USER_SESSION_NAME, user);
		mav.addObject("user", user);
		mav.addObject("leaf", "center");
		return mav;
	}
	
	@RequestMapping("/euser/resetPwd")//页面不用变
	public ModelAndView resetPwd(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/euser/resetPwd.httl");
		setVar(mav);
		EUser user = (EUser) this.getSessionAttribute(request,
				CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			mav.setViewName("redirect:/login");
			return mav;
		}
		mav.addObject("user", user);
		mav.addObject("leaf", "resetPwd");
		return mav;
	}

	@RequestMapping("/euser/commitePwd")//页面不用变
	public ModelAndView commitePwd(HttpServletRequest request,
			HttpServletResponse response, ResetPwdParams pwd) {
		ModelAndView mav = new ModelAndView("redirect:/login");
		setVar(mav);
		EUser user = (EUser) this.getSessionAttribute(request,
				CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			return mav;
		}
		EUser reUser = euserService.getByUserName(user);
		if (MD5.getMD5(pwd.getOldPassword()).equals(reUser.getPassword())) {
			reUser.setPassword(MD5.getMD5(pwd.getNewPassword()));
			euserService.save(reUser);
			this.clear(request, response);
		} else {
			mav.setViewName("redirect:/euser/resetPwd");
			mav.addObject("oldPwdError", true);
			return mav;
		}
		return mav;
	}
}
